

var http 		= require('http')
,	express 	= require('express')
,	mongodb 	= require('mongodb')
,	async 		= require('async')
,	fs 			= require('fs')
,	sauberfant	= require('./lib/sauberfant')
,	sf			= require('./lib/options')
,	server 		= new mongodb.Server(sf.options.mongo.server, sf.options.mongo.port, {})
,	db			= new mongodb.Db(sf.options.mongo.database, server, {})
,	port 		= process.env.PORT || 3000;


db.open(function (error, client) {

	db.authenticate(sf.options.mongo.username, sf.options.mongo.password, function(){

		if (error) throw error;


		var app		= express.createServer()
		,	sf		= new sauberfant.Sauberfant(client);

		// Build sf Script on every server launch
		sf.build();


		app.configure(function(){
			app.use(express.static(__dirname + '/../public'));
		});

		app.set('view options', {
			layout: false
		});
		app.set('views', __dirname + '/views');

		app.get('/collector', function (req, res) {
			sf.collect(req, res);
		});

		app.get('/visualiser', function (req, res) {
			sf.process(req, res);
		});


		app.get('/overlay', function (req, res) {
			sf.overlay(req, res);
		});

		app.listen(port, function() {
			console.log("Listening on " + port);
		});

	});
});
